package cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.demo.exection;

import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmConstants;
import org.checkerframework.checker.units.qual.A;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 雨同我
 * @Description:
 * @DateTime: 2024/8/22 16:58
 **/

@Component
public class SkipTaskExpress implements TaskListener {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;

    @Override
    public void notify(DelegateTask delegateTask) {
        // 获取流程定义 ID
        String processDefinitionId = delegateTask.getProcessDefinitionId();
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();

        // 获取 BPMN 模型
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);

        // 获取特定的用户任务
        UserTask userTask = (UserTask) bpmnModel.getFlowElement(taskDefinitionKey);

//        updateTaskStatusAndReason(delegateTask.getId(), BpmTaskStatusEnum.APPROVE.getStatus(), "审批人为空，任务自动跳过");

        taskService.complete(delegateTask.getId());
        System.out.println("任务+=="+delegateTask.getAssignee()+"任务id+ "+delegateTask.getId());
    }


    private void updateTaskStatusAndReason(String id, Integer status, String reason) {
        taskService.setVariableLocal(id, BpmConstants.TASK_VARIABLE_STATUS, status);
        taskService.setVariableLocal(id, BpmConstants.TASK_VARIABLE_REASON, reason);
    }


}
